<?php

namespace App\Admin\Controllers\Vote\Chapter;

use App\Admin\Actions\Batches\BatchDisable;
use App\Admin\Actions\Batches\BatchEnable;
use App\Admin\Actions\GoBackButton;
use App\Admin\Actions\Player\BatchUpgradePoster;
use App\Admin\Actions\Player\BatchUpload;
use App\Admin\Actions\Player\UpgradeLink;
use App\Admin\Actions\Player\UpgradePoster;
use App\Admin\Renders\CommonGrid;
use App\Admin\Traits\WithUploads;
use App\Models\Chapter;
use App\Models\Gift;
use App\Models\Player;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;

class PlayerController extends Controller
{
    use WithUploads;

    public function index(Content $content, Chapter $chapter): Content
    {
        return $content->header('选手管理')
            ->description($chapter->title)
            ->body($this->grid($chapter));
    }

    public function grid(Chapter $chapter): Grid
    {
        $model = Player::ofAdmin()
            ->where('chapter_id', $chapter->getKey())
            ->with(['chapter'])
            ->orderByDesc('polls');

        return Grid::make($model, function (Grid $grid) use ($chapter) {
            $grid->disableBatchDelete(false);
            $grid->batchActions([
                new BatchUpgradePoster(),
                new BatchEnable(Gift::class),
                new BatchDisable(Gift::class),
            ]);
            $grid->actions([
                new UpgradePoster,
                new UpgradeLink,
            ]);

            $grid->tools([new BatchUpload($chapter), new GoBackButton]);
            $grid->column('排名')
                ->display(fn() => $this->rank);
            CommonGrid::player($grid);
        });
    }

    public function create(Content $content, Chapter $chapter): Content
    {
        return $content->header('新增选手')
            ->description($chapter->title)
            ->body($this->form($chapter));
    }

    public function form(Chapter $chapter): Form
    {
        return Form::make(Player::ofAdmin(), function (Form $form) use ($chapter) {
            $form->block(8, function (Form\BlockForm $form) use ($chapter) {
                $form->hidden('chapter_id')
                    ->value($chapter->getKey());
                $form->title('基础信息');
                $form->showFooter();
                $form->text('name', '选手姓名')
                    ->required();
                $form->switch('status')
                    ->default(true);
                $form->editor('description', '选手介绍')
                    ->imageUrl(route('admin.upload.editor'));
            });
            $form->block(4, function (Form\BlockForm $form) {
                $form->title('图片信息');
                $this->cover($form, 'cover', '选手头像')
                    ->dimensions(['min_width' => 375, 'max_width' => 750])
                    ->help('图片宽度请在375~750px之间')
                    ->required();
                $this->pictures($form)
                    ->dimensions(['min_width' => 375, 'max_width' => 750])
                    ->help('图片宽度请在375~750px之间');
            });
        });
    }

    public function edit(Content $content, Chapter $chapter, int $id): Content
    {
        return $content->header('编辑选手')
            ->description($chapter->title)
            ->body($this->form($chapter)->edit($id));
    }

    public function update(Chapter $chapter, int|string $id): JsonResponse
    {
        return $this->form($chapter)->update($id);
    }

    public function store(Chapter $chapter): JsonResponse
    {
        return $this->form($chapter)->store();
    }

    public function destroy(Chapter $chapter, int|string $id): JsonResponse
    {
        return $this->form($chapter)->destroy($id);
    }
}
